/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ytmc.uploader;

import com.google.gdata.client.GoogleService.CaptchaRequiredException;
import com.google.gdata.util.AuthenticationException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import ytmc.Controller;
import ytmc.View;

/**
 *
 * @author K
 */
public class UploaderController implements Controller
{
    private UploaderModel model;
    private UploaderView view;

    private AddUploadTasksModel addTaskModel;
    private AddUploadTasksView addTaskView;


    public UploaderController()
    {
        this.model = new UploaderModel();
        this.view = new UploaderView(this, model);

        this.addTaskModel = new AddUploadTasksModel();
        this.addTaskView = new AddUploadTasksView(this, addTaskModel);

    }

    public void showAddUploadTask()
    {
        addTaskModel.clearTasks();
        addTaskView.clear();
        addTaskView.setVisible(true);
    }

    public void addTasks()
    {
        model.addTasks(addTaskModel.getTasksToAdd());
    }

    public void removeTask(int index)
    {
        model.removeTask(index);
    }

    public void startTask(int index)
    {
        try
        {
            model.startTask(index);
        }
        catch (CaptchaRequiredException ex)
        {
            CaptchaView captchaView = new CaptchaView(this, ex.getCaptchaToken(), ex.getCaptchaUrl());
            captchaView.setVisible(true);
            Logger.getLogger(UploaderController.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (AuthenticationException ex)
        {
            view.showMessage(ex.getMessage());
            Logger.getLogger(UploaderController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void submitCaptcha(String captchaToken, String captchaAnswer)
    {
        try
        {
            model.submitCaptcha(captchaToken, captchaAnswer);
        }
        catch (CaptchaRequiredException ex)
        {
            CaptchaView captchaView = new CaptchaView(this, ex.getCaptchaToken(), ex.getCaptchaUrl());
            captchaView.setVisible(true);
            Logger.getLogger(UploaderController.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (AuthenticationException ex)
        {
            view.showMessage(ex.getMessage());
            Logger.getLogger(UploaderController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addTasksFromPath(String path)
    {
        try
        {
            addTaskModel.addTasksFromPath(path);
        }
        catch (IOException ex)
        {
            view.showMessage(ex.getMessage());
            Logger.getLogger(UploaderController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void removeSelectedTask()
    {
        addTaskModel.removeSelectedTask();
    }

    public void setSelectedTask(int index)
    {
        addTaskModel.setSelectedTask(index);
    }

    public void setTitle(String title)
    {
        addTaskModel.getSelectedTask().setTitle(title);
        addTaskView.canAccept(addTaskModel.areTasksValid());
    }

    public void setCategory(VideoCategory category)
    {
        addTaskModel.getSelectedTask().setCategory(category);
    }

    public void setTags(String tags)
    {
        addTaskModel.getSelectedTask().setTags(tags);
    }

    public void setDescription(String desc)
    {
        addTaskModel.getSelectedTask().setDescription(desc);
    }

    @Override
    public View getView()
    {
        return view;
    }
}
